/*
 * @Description: 最近使用Hook
 * @Author: zhaoxiaoyang & seadon
 * @LastEditors: seadon
 * @Date: 2021-07-19 21:33:05
 * @LastEditTime: 2021-07-21 14:41:15
 */
import { ref } from 'vue'
import { apiInit } from '@/services'
import { useLoading } from '@/hooks/useLoading'
import { isMoreTabs } from '@/utils/common'
export default function useRecently() {
	const api = apiInit()
	const recentList = ref({})
	const defaultLoading = ref({ loading: false })
	const { getLoading, setLoading } = useLoading(defaultLoading)

	const fetchRecentList = async () => {
		setLoading(true)
		const res = await api.default.ListRecentPrivateV2POST({
			query: {
				enableMultipleTab: isMoreTabs(),
			},
		})
		setLoading(false)
		const data = res.data?.map((item: any) => {
			return {
				name: item.name,
				id: item.resource_id,
				link: item.link,
				date: item.recently_use_time,
				icon: item.icon || '',
				sapp_type: item.sapp_type,
			}
		})
		recentList.value = {
			type: 'recent',
			data,
		}
	}

	return {
		getLoading,
		recentList,
		fetchRecentList,
	}
}
